<?php
/**
 * Created by PhpStorm.
 * User: 刘红波 <liuhb@cqjcdflzxsws.com>
 * Date: 2018/11/20
 * Time: 14:51
 */

namespace app\api\until\es;


use app\api\until\ESearch;
use think\Exception;
use app\api\until\es\ESConf as ES;
class Doc extends ESearch implements ES
{
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 创建
     * @return array
     */
    public function createDoc()
    {
        return $this->getClient()->index($this->data);
    }

    /**
     *
     * 获取
     * param
     *
     * @return array
     */
    public function getDoc(){
        return $this->getClient()->get($this->data);
    }

    /**
     *搜索
     * @return array
     */
    public function searchDoc(){
        return $this->getClient()->search($this->data);
    }


    /**
     * 更新
     * @return array
     */
    public function updateDoc(){
        return $this->getClient()->update($this->data);
    }

    /**
     * 删除
     * @return array
     */
    public function deleteDoc(){
        return $this->getClient()->delete($this->data);
    }

    /**
     *批量删除
     * @return array
     */
    public function deleteByBatch(){
        return $this->getClient()->bulk($this->data);
    }


    /**
     * @param $type
     * @param array $data
     * @return array
     * @throws Exception
     */
    public function beforeBulk($type, array $data){
        if (!is_array($data)){
            throw new Exception('$data必须是一个一维数组');
        }
        $arr = [];
        foreach ($data as $datum){
            $temp['_index'] = ES::TOPIC_INDEX;
            $temp['_type'] = ES::TOPIC_TYPE;
            $temp['_id'] = $datum;
            $arr[] = [$type=>$temp];
            unset($temp);
        }
        return $arr;
    }
}